﻿using Demo.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using Model.Dto;
using Model.Models;
using Service;
using System;

namespace Demo.Controllers
{
    public class ProductController : Controller
    {
        private IProductService _productService;
        public ProductController(IProductService productService)
        {
            _productService = productService;
        }
        //首页商品列表
        public ActionResult Index(IndexRequest request)
        {
            ViewData["CurrentSort"] = request.SortOrder;
            ViewData["PriceSortOrder"] = String.IsNullOrEmpty(request.SortOrder) ? "price_desc" : "";
            ViewData["TimeSortOrder"] = request.SortOrder == "Time" ? "time_desc" : "Time";

            if (request.SearchString != null)
                request.Page = 1;
            else
                request.SearchString = request.CurrentFilter;

            ViewData["CurrentFilter"] = request.SearchString;
            var list = _productService.GetAll(request.SortOrder, request.SearchString);
            return View(PageList<Product>.Create(list, request.Page, request.Size));
        }
        /// <summary>
        /// 初始化数据（30条）
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ActionResult NewProduct(int pageIndex = 1, int pageSize = 10)
        {
            _productService.CreateNewProduct();
            var list = _productService.GetAll();
            return View("Index", (PageList<Product>.Create(list, pageIndex, pageSize)));
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            return View();
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ActionResult Delete(int id, int pageIndex = 1, int pageSize = 10)
        {
            _productService.Delete(id);
            var list = _productService.GetAll();
            return View("Index", (PageList<Product>.Create(list, pageIndex, pageSize)));
        }
    }
}
